﻿/**********************************************************************************************************************
 * 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an 
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * 
 * 注:部分代码来自于以下开源社区 
 * SubSonic Open Source - http://subsonicproject.com
 * SubSonic_2.1_Final_Source
 * 
 *********************************************************************************************************************/



using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web;
using System.Web.UI;

namespace Brandy.Utilites
{
    /// <summary>
    /// HttpContext 对象的辅助处理类 
    /// </summary>
    public class StateHelper
    {

        #region Session Helpers

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetSessionValue<T>(string key)
        {
            return GetSessionValue<T>(HttpContext.Current, key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetSessionValue<T>(HttpContext context, string key)
        {
            if (context == null)
                return default(T);
            object o = context.Session[key];
            if (o == null)
                return default(T);
            return (T)o;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetSessionValue(string key, object value)
        {
            SetSessionValue(HttpContext.Current, key, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetSessionValue(HttpContext context, string key, object value)
        {
            if (context == null)
                return;
            context.Session[key] = value;
        }

        #endregion

        #region Context Helpers

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetContextValue<T>(string key)
        {
            return GetContextValue<T>(HttpContext.Current, key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetContextValue<T>(HttpContext context, string key)
        {
            if (context == null)
                return default(T);
            object o = context.Items[key];
            if (o == null)
                return default(T);
            return (T)o;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetContextValue(string key, object value)
        {
            SetContextValue(HttpContext.Current, key, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetContextValue(HttpContext context, string key, object value)
        {
            if (context == null)
                return;
            context.Items[key] = value;
        }

        #endregion

        #region ViewState Helpers

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static StateBag GetStateBag()
        {
            if (HttpContext.Current == null)
                return null;

            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page == null)
                return null;

            Type pageType = typeof(Page);
            PropertyInfo viewState = pageType.GetProperty("ViewState", BindingFlags.GetProperty | BindingFlags.Instance);
            if (viewState == null)
                return null;

            return viewState.GetValue(page, null) as StateBag;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetViewStateValue<T>(string key)
        {
            return GetViewStateValue<T>(GetStateBag(), key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bag"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetViewStateValue<T>(StateBag bag, string key)
        {
            if (bag == null)
                return default(T);
            object o = bag[key];
            if (o == null)
                return default(T);
            return (T)o;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetViewStateValue(string key, object value)
        {
            SetViewStateValue(GetStateBag(), key, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bag"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetViewStateValue(StateBag bag, string key, object value)
        {
            if (bag != null)
                bag[key] = value;
        }

        #endregion

        #region Cookie Helpers

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool HasCookieValue(string key)
        {
            return !string.IsNullOrEmpty(GetCookieValue(HttpContext.Current, key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCookieValue(string key)
        {
            return GetCookieValue(HttpContext.Current, key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCookieValue(HttpContext context, string key)
        {
            // Updated by NH to check against session values as well, which is an optional switch used by members
            string tempValue = null;
            if (context == null || context.Request == null)
                return null;

            HttpCookie cookie = context.Request.Cookies[key];
            if (cookie == null)
            {
                // Check for session
                if (context.Session[key] != null)
                    if (context.Session[key].ToString() != "0")
                        tempValue = context.Session[key].ToString();
            }
            else
                tempValue = cookie.Value;

            return tempValue;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookieValue(string key, string value)
        {
            SetCookieValue(HttpContext.Current, key, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookieValue(HttpContext context, string key, string value)
        {
            if (context == null || context.Request == null)
                return;
            HttpCookie cookie = context.Request.Cookies[key];
            if (cookie == null)
                cookie = new HttpCookie(key);

            cookie.Value = value;

            // add default exp on a month
            cookie.Expires = DateTime.Now.AddMonths(1);

            // if cookie exists, remove
            context.Response.Cookies.Add(cookie);
        }

        #endregion

    }
}
